const booksModule = {
    // 加载并显示图书列表
    loadBooks() {
        const books = dataModule.getAllBooks();
        const container = document.getElementById('books-container');
        container.innerHTML = '';

        books.forEach(book => {
            const bookCard = this.createBookCard(book);
            container.appendChild(bookCard);
        });

        this.initializeFilters();
    },

    createBookCard(book) {
        const card = document.createElement('div');
        card.className = 'book-card';
        
        const statusClass = book.status === '可借阅' ? 'available' : 'borrowed';
        const bookIcon = this.getBookIcon(book.category);
        const categoryColor = this.getCategoryColor(book.category);
        
        card.innerHTML = `
            <div class="book-cover" style="background: ${categoryColor}">
                <div class="book-icon-wrapper">
                    <i class="fas ${bookIcon}"></i>
                    <span class="category-label">${book.category}</span>
                </div>
            </div>
            <div class="book-info">
                <h3>
                    ${book.title}
                    <span class="status ${statusClass}">${book.status}</span>
                </h3>
                <div class="book-meta">
                    <div class="meta-item">
                        <i class="fas fa-user"></i>
                        <span>${book.author}</span>
                    </div>
                    <div class="meta-item">
                        <i class="fas fa-calendar"></i>
                        <span>${book.publishYear}</span>
                    </div>
                    <div class="meta-item">
                        <i class="fas fa-tag"></i>
                        <span>${book.category}</span>
                    </div>
                </div>
                <div class="actions">
                    ${book.status === '可借阅' ? 
                        `<button class="borrow-btn" onclick="booksModule.borrowBook(${book.id})">
                            <i class="fas fa-hand-holding"></i>
                            借阅
                        </button>` : 
                        ''}
                    <button class="detail-btn">
                        <i class="fas fa-info-circle"></i>
                        详情
                    </button>
                </div>
            </div>
        `;
        return card;
    },

    getBookIcon(category) {
        const icons = {
            '文学': 'fa-book-open',
            '科幻': 'fa-rocket',
            '计算机': 'fa-laptop-code',
            'default': 'fa-book'
        };
        return icons[category] || icons.default;
    },

    getCategoryColor(category) {
        const colors = {
            '文学': 'linear-gradient(135deg, #1890ff 0%, #36cfc9 100%)',
            '科幻': 'linear-gradient(135deg, #722ed1 0%, #f759ab 100%)',
            '计算机': 'linear-gradient(135deg, #fa8c16 0%, #faad14 100%)',
            'default': 'linear-gradient(135deg, #1890ff 0%, #36cfc9 100%)'
        };
        return colors[category] || colors.default;
    },


    initializeFilters() {
        const searchInput = document.getElementById('search-input');
        const categoryFilter = document.getElementById('category-filter');
        const statusFilter = document.getElementById('status-filter');

        const filterBooks = () => {
            const searchTerm = searchInput.value.toLowerCase();
            const category = categoryFilter.value;
            const status = statusFilter.value;

            const books = dataModule.getAllBooks().filter(book => {
                const matchesSearch = book.title.toLowerCase().includes(searchTerm) ||
                                    book.author.toLowerCase().includes(searchTerm);
                const matchesCategory = !category || book.category === category;
                const matchesStatus = !status || book.status === status;

                return matchesSearch && matchesCategory && matchesStatus;
            });

            const container = document.getElementById('books-container');
            container.innerHTML = '';
            books.forEach(book => {
                const bookCard = this.createBookCard(book);
                container.appendChild(bookCard);
            });
        };

        searchInput.addEventListener('input', filterBooks);
        categoryFilter.addEventListener('change', filterBooks);
        statusFilter.addEventListener('change', filterBooks);
    },

    borrowBook(bookId) {
        const book = dataModule.getBookById(bookId);
        if (book && book.status === '可借阅') {
            book.status = '已借出';
            // 添加借阅记录
            dataModule.addBorrowing(bookId);
            this.loadBooks();
            alert(`成功借阅《${book.title}》`);
        } else {
            alert('该图书无法借阅！');
        }
    }
};

document.addEventListener('DOMContentLoaded', () => {
    booksModule.loadBooks();
}); 